package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    //根据用户名获取菜单列表
    public List<Menu> getMenuListByUsername(String username) {
        //获取1级菜单
        List<Menu> menuList = menuDao.getMenuListByUsername(username);
        //遍历一级菜单，添加二级菜单
        for (Menu menu : menuList) {
            Integer id = menu.getId();
            //根据关联id查询子菜单
            List<Menu> childList = menuDao.getChildListById(id);
            //设置属性
            menu.setChildren(childList);
        }
        return menuList;
    }

    //分页查询
    public PageResult findPage(QueryPageBean queryPageBean) {
        //获取分页查询参数
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);//分页助手
        Page<Menu> page = menuDao.findByCondition(queryString);

        List<Menu> menuList = page.getResult();
        for (Menu menu : menuList) {
            //遍历一级菜单，添加二级菜单
                Integer id = menu.getId();
                //根据关联id查询子菜单
                List<Menu> childList = menuDao.getChildListById(id);
                //设置属性
                menu.setChildren(childList);
        }
        return new PageResult(page.getTotal(),page.getResult());
    }

    //查询所有一级菜单id
    public List<Menu> findParentMenuId() {
        return menuDao.findByCondition("");
    }

    //根据菜单id查询菜单
    public Menu findMenuById(Integer id) {
        return menuDao.findMenuById(id);
    }

    //编辑菜单
    public void edit(Menu menu) {
        //暂时不考虑1级菜单改2级，2级菜单改1级问题
        menuDao.edit(menu);
    }

    //新增菜单
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    //删除菜单
    public void delete(Integer id) {
        //根据当前菜单id查询是否是其他菜单的parentMenuId
        long count = menuDao.selectCountByMenuId(id);
        if(count > 0){
            //已经被自关联，不能删除
            throw new RuntimeException(MessageConstant.DELETE_MENU_FAIL);
        }else{
            //查询该菜单是否已被角色关联
            Long count2 = menuDao.selectCountByMenu_Id4Role(id);
            if (count2 > 0){
                //删除关联关系
                menuDao.deleteMenuIdRelation(id);
//                throw new RuntimeException(MessageConstant.DELETE_MENU_FAIL);
            }
            //可以删除
            menuDao.deleteById(id);
        }
    }

    //获取该父节点的所有子节点数据集合
    public List<Menu> getTreeNodeByParentId(Integer pid) {
        return menuDao.getChildListById(pid);
    }

}
